/**
 * 
 */
package com.brewdroid.db.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.brewdroid.db.DatabaseHelper;
import com.brewdroid.enumerators.HopClass;
import com.brewdroid.enumerators.HopFormat;
import com.brewdroid.model.Hop;

/**
 * @author meeusen
 *
 */
public class HopDAO {

	private static final String HOP_SELECT_ALL="select * from hop order by name";
	private static final String HOP_SELECT_BY_ID="select * from hop h where h._id =?";
	private static final String HOP_SELECT_BY_NAME="select * from hop h where name=?";
	private static final String HOP_SELECT_ID="select _id from hop h where h.classification=? and h.format=? and h.name=? and h.description=?";
	private static final String TABLE="hop";
	private static final String NULL_COL="name";
	
	
	public static void addHops(DatabaseHelper db, ArrayList<Hop> hops)
	{
		for(Hop h:hops)
			addHop(db,h);
	}
	
	public static Hop getHopByName(DatabaseHelper db, String name)
	{
		Hop h = null;
		Cursor c = db.getReadableDatabase().rawQuery(HOP_SELECT_BY_NAME, new String[]{name});
		
		if(c.moveToFirst())
			h=makeHopFromCursor(c);
		return h;
	}
	
	public static Hop getHopById(DatabaseHelper db, int id)
	{
		Hop h = new Hop();
		Cursor cursor = db.getReadableDatabase().rawQuery(HOP_SELECT_BY_ID, new String[]{Integer.toString(id)});
		if(cursor.moveToFirst())
			h=makeHopFromCursor(cursor);				
		
		return h;	
	}
	
	
	public static void addHop(DatabaseHelper db, Hop hop)
	{
		if(getId(db, hop)==0)	//only add hops not in db already
		{
			ContentValues cv= new ContentValues();
			cv.put("description", hop.getDescription());
			cv.put("alphaAcid", hop.getAlphaAcid());
			cv.put("betaAcid", hop.getBetaAcid());
			cv.put("classification", hop.getClassification().toString().toLowerCase());
			cv.put("format", hop.getFormat().toString().toLowerCase());
			cv.put("name", hop.getName());
		
			db.getWritableDatabase().insert(TABLE,NULL_COL,cv);
		}
	}
	
	public static int getId(DatabaseHelper db, Hop hop)
	{			
		Cursor cursor= db.getReadableDatabase().rawQuery(HOP_SELECT_ID, new String[]{	hop.getClassification().toString().toLowerCase(),
																						hop.getFormat().toString().toLowerCase(),
																						hop.getName(),
																						hop.getDescription()
																					});
		int id=0;
		if(cursor.moveToFirst())
			id=cursor.getInt(0);		

		return id;
	}
	
	public static ArrayList<Hop> getHops(DatabaseHelper db)
	{
		ArrayList<Hop> hops = new ArrayList<Hop>();
		Cursor cursor =db.getReadableDatabase().rawQuery(HOP_SELECT_ALL, new String[]{});
		if(cursor.moveToFirst())	//if there are any entries in cursor
		{
			do
			{
				Hop aHop =makeHopFromCursor(cursor);
				hops.add(aHop);
			}
			//while there are more hops continue
			while(cursor.moveToNext());
		}
		return hops;
	}
	
	public static ArrayList<String> getHopStrings(DatabaseHelper dbHelper) {
		ArrayList<String> hopStrings = new ArrayList<String>();
		for(Hop i: getHops(dbHelper)){
			hopStrings.add(i.getName());
		}
		
		return hopStrings;
	}
	
	private static Hop makeHopFromCursor(Cursor cursor)
	{
		Hop aHop= new Hop();
		
		aHop.setDescription(cursor.getString(5));
		aHop.setName(cursor.getString(6));
		aHop.setAlphaAcid(cursor.getFloat(1));
		aHop.setBetaAcid(cursor.getFloat(2));

		String hopCla=cursor.getString(3);
		String hopFor=cursor.getString(4);
		
		//determine and set hop classification
		if(hopCla.equalsIgnoreCase("flavor"))
			aHop.setHopClass(HopClass.FLAVOR);
		else if(hopCla.equalsIgnoreCase("bittering"))
			aHop.setHopClass(HopClass.BITTERING);
		else if(hopCla.equalsIgnoreCase("aroma"))
			aHop.setHopClass(HopClass.AROMA);
		else if(hopCla.equalsIgnoreCase("dualuse"))
			aHop.setHopClass(HopClass.DUALUSE);
		//determine and set hop format
		if(hopFor.equalsIgnoreCase("pellet"))
			aHop.setFormat(HopFormat.PELLET);
		else if(hopFor.equalsIgnoreCase("whole"))
			aHop.setFormat(HopFormat.WHOLE);
		else if(hopFor.equalsIgnoreCase("cone"))
			aHop.setFormat(HopFormat.CONE);
		else if(hopFor.equalsIgnoreCase("oil"))
			aHop.setFormat(HopFormat.OIL);
		//add hop to list
		
		return aHop;
	}
}
